package com.wxl.common.adapter

import android.view.KeyEvent
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.wxl.common.R
import com.wxl.common.base.BaseEventKeyBean
import com.wxl.common.util.Toll
import com.wxl.common.wiget.TopSmoothScroller

abstract class AbsKeyEventAdapter<T : BaseEventKeyBean> : BaseQuickAdapter<T, BaseViewHolder> {

    open var position = -1
    open var pageSize = 15
    private var isLastPage = false
    private var isFirstPage = false

    constructor(layoutResId: Int, data: List<T>?) : super(layoutResId, data)
    constructor(data: List<T>?) : super(data)
    constructor(layoutResId: Int) : super(layoutResId)


    override fun convert(helper: BaseViewHolder, item: T) {
        helper.itemView.setBackgroundResource(if (item.isSelect) R.drawable.shape_unapply_list_item_focus else R.drawable.shape_unapply_list_item)
    }

    fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            movePreSelect()
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            moveNextSelect()
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            movePrePage()
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            moveNextPage()
        }
        return false
    }


     fun getBindView(): RecyclerView? {
        return super.getRecyclerView()
    }

    /**
     * 获得被选中的position
     */
    fun getSelectedPosition(): Int {
        return position
    }

    /**
     * 往下移一位
     */
    private fun moveNextSelect() {
        if (position >= data.size - 1) {
            return
        }
        if (position >= 0) {
            data[position].isSelect = false
        }
        position++
        data[position].isSelect = true
        notifyDataSetChanged()
        recyclerView.layoutManager?.scrollToPosition(position)
    }


    /**
     * 往上移一位
     */
    private fun movePreSelect() {
        if (position <= 0 || position >= data.size) {
            return
        }
        if (position >= 0) {
            data[position].isSelect = false
        }
        position--
        data[position].isSelect = true
        notifyDataSetChanged()
        recyclerView.layoutManager?.scrollToPosition(position)
    }

    /**
     *  移动到下一页
     */
    fun moveNextPage() {
        var p = (position / pageSize) + 1
        var n = (p + 1) * pageSize
        if (position >= 0) {
            data[position].isSelect = false
        }
        if (n < data.size) {
            isLastPage = false
            recyclerView.layoutManager?.scrollToPosition(n)
            recyclerView.postDelayed(Runnable {
                position = n - pageSize - 1
                moveNextSelect()
            }, 100)
        } else {
            if (n - pageSize <= data.size) {
                recyclerView.layoutManager?.let {
                    it as LinearLayoutManager
                    val mScroller = TopSmoothScroller(recyclerView.context)
                    mScroller.setTargetPosition(data.size - 1)
                    it.startSmoothScroll(mScroller)
                }
                recyclerView.postDelayed(Runnable {
                    if (n >= data.size) {
                        position = p * pageSize - 1
                    } else {
                        position = data.size - p * pageSize - 1
                    }
                    moveNextSelect()
                    isLastPage = true
                    Toll.toll("最后一页")
                }, 100)
            } else {
                isLastPage = true
                Toll.toll("最后一页")
            }
        }
    }


    /**
     * 是否是最后一页
     */
    fun isLastPage(): Boolean {
        if (data.size < pageSize) {
            return true
        }
        return isLastPage
    }


    /**
     * 是否是第一页
     */
    fun isFirstPage(): Boolean {
        if (data.size < pageSize) {
            return true
        }
        return isFirstPage
    }

    /**
     * 移动到上一页
     */
    fun movePrePage() {
        var p = 1
        if (position > 0) {
            p = position / pageSize
        }
        if (p <= 0) {
            isFirstPage = true
            return
        }
        isFirstPage = false
        var n = (p - 1) * pageSize
        //Loog.e("pos:$position p:$p n:$n")
        if (n < data.size) {
            recyclerView.layoutManager?.let {
                val mScroller = TopSmoothScroller(recyclerView.context)
                mScroller.targetPosition = n
                it.startSmoothScroll(mScroller)
            }
            if (position > 0) {
                data[position].isSelect = false
            }
            recyclerView.postDelayed(Runnable {
                if (n < pageSize) {
                    position = -1
                    notifyDataSetChanged()
                } else {
                    position = n - 1
                }
                moveNextSelect()
            }, 120)
        }
    }


    fun selectItem(position: Int) {
        if (data.size > position) {
            if (this.position >= 0 && this.position < data.size) {
                data[this.position].isSelect = false
                notifyItemChanged(this.position)
            }
            this.position = position
            data[this.position].isSelect = true
            notifyItemChanged(position)
        }
    }

}